<?php
        require_once('auth/auth.php');
?>

<?php

$_SESSION['LAUNCH_OK'] = '';
$history = array();

function fail($errmsg){
	$msg = array();
	$msg = $errmsg;
	$_SESSION['ERRMSG_ARR'] = $msg;
//      session_write_close();
        header("location: launch.php");
	exit();
}

function record($rec) {
	$history[] = $rec;
}

$EXP_UID = $_SESSION['SESS_USER_ID'];

$STORE_DIR="/mnt/workspace/data";
$STORE_NAME=$EXP_UID . "_" . time() . substr(md5(uniqid(rand(), true)),0,10);
$STORE_FILE=$STORE_DIR . "/" . $STORE_NAME;

if (($_FILES["expfile"]["type"] == "text/plain") && ($_FILES["expfile"]["size"] < 20000))
  {

    if ($_FILES["expfile"]["error"] > 0) {
	fail("Return Code: " . $_FILES["expfile"]["error"] . "<br />");

    } else {
	
	record("Upload: " . $_FILES["expfile"]["name"] . "<br />");
	record("Type: " . $_FILES["expfile"]["type"] . "<br />");
	record("Size: " . ($_FILES["expfile"]["size"] / 1024) . " Kb<br />");
	record("Temp file: " . $_FILES["expfile"]["tmp_name"] . "<br />");

//	if (file_exists($UPLOAD_DIR . $_FILES["expfile"]["name"])) {
	if (file_exists($STORE_FILE)) {
//		echo $_FILES["expfile"]["name"] . " already exists. ";
	//	echo $STORE_FILE . " already exists. ";
	} else {
	
      		move_uploaded_file($_FILES["expfile"]["tmp_name"], $STORE_FILE); 
		#$UPLOAD_DIR . "/" . $_FILES["expfile"]["name"]);
//    		echo "Stored in: " . $UPLOAD_DIR . "/" . $_FILES["expfile"]["name"];
    		//echo "Stored in: " . $STORE_FILE;
      }
    }
  } else {
	  fail("Invalid file. Only a plain text numbers list is accepted.");
  }


// File OK!

	 // Check OP
	$EXP_OP = 0;
	if(isset($_POST['bexpident']) && (($_POST['bexpident']) == '1')) $EXP_OP=+1;
	if(isset($_POST['bexpfactor']) && (($_POST['bexpfactor']) == '1')) $EXP_OP+=2;
	if($EXP_OP == 0) $EXP_OP = 3;

// Connect DB

       //Include database connection details
        require_once('db/config-exp.php');

        //Connect to mysql server
        $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
        if(!$link) {
        	fail("Could not conect to server.");
        }

        //Select database
        $db = mysql_select_db(DB_DATABASE);
        if(!$db) {
                fail("Could not connect to DB.");
	}

	//Check experiment name
	$EXP_NAME = $_POST['expname'];	

	if($db) {

	        $qry=	"INSERT INTO experiment_web (user_id, name, op, file_name) " .
			" VALUES (".$EXP_UID.",'".$EXP_NAME."',". $EXP_OP .",'".$STORE_FILE."');";
		$result = @mysql_query($qry);

		    //Chdeck whether the query was successful or not
		    if(!$result) {
			fail("Query failed! (" . $qry . ")");
		}
	 
		$qry =	"SELECT id FROM experiment_web WHERE  file_name='" . $STORE_FILE . "';";
		$result = mysql_query($qry);
		
		if($result) {
			if(mysql_num_rows($result) >= 1) {
 
				$result = mysql_fetch_array($result);
				$EXP_ID = $result['id'];

		  	} else {
				fail("Demasiada casualidad no crees");
			}
		} else {
			fail("Query failed! (" . $qry . ")");
		}
        }

	$command = '/opt/experiment/script/expmanager-client-send.sh';
	$args = $EXP_ID . " " . $EXP_UID . " " . $STORE_FILE;

	$shit = $command . " " . $args;

	passthru($command . " " . $args);

	
	$_SESSION['LAUNCH_OK'] = $history;
	header('Location: launch-ok.php');
?>
